#include "stdafx.h"
#include "ChatContexView.h"



namespace DuiLib
{
	IMPLEMENT_DUICONTROL(CChatContextViewUI)

	CChatContextViewUI::CChatContextViewUI()
		: m_ColumnCount(0)
	{
	}

	CChatContextViewUI::~CChatContextViewUI()
	{
	}

	LPCTSTR CChatContextViewUI::GetClass() const
	{
		return _T("ChatContextViewUI");
	}

	LPVOID CChatContextViewUI::GetInterface(LPCTSTR pstrName)
	{
		if (_tcsicmp(pstrName, _T("ChatContextView")) == 0) return static_cast<CChatContextViewUI*>(this);
		return CContainerUI::GetInterface(pstrName);
	}

	void CChatContextViewUI::setModel(CAbstractItemModel* pModel)
	{
		CAbstractItemView::setModel(pModel);

		if (pModel)
		{
			showModel();
		}
	}

	void CChatContextViewUI::setItemSource(const CDuiString& itemSource)
	{
		m_ItemSource = itemSource;
	}

	void CChatContextViewUI::onDataChanged(const CModelIndex& index,
		const CVar& oldValue, const CVar& newValue)
	{
		setItemData(index);
	}

	void CChatContextViewUI::onRemoveAll(int rowCount)
	{
		RemoveAll();
	}
	
	void CChatContextViewUI::onRowsBefroreInsert(int row, int count, const CModelIndex& parent)
	{

	}

	void CChatContextViewUI::onRowsInserted(int row, int count, const CModelIndex& parent)
	{
		addLineToView();

		EndDown();
	}

	void CChatContextViewUI::onRowsBefroreRemove(int row, int count, const CModelIndex& parent)
	{
	}

	void CChatContextViewUI::onRowsRemoved(int row, int count, const CModelIndex& parent)
	{
		for (int i = count; i > 0; i++)
		{
			RemoveAt(row + i);
		}
	}

	void CChatContextViewUI::showModel()
	{
		RemoveAll();

		CAbstractItemModel* pModel = model();
		if (!pModel)
		{
			return;
		}

		for (int row = 0; row < pModel->rowCount(); row++)
		{
			CContainerUI* pItem = (CContainerUI*)addLineToView();
			if (!pItem)
			{
				continue;
			}

			for (int col = 0; col < pModel->columnCount(); col++)
			{
				CModelIndex&& index = pModel->index(row, col);
				setItemData(index);
			}
		}
	}


	void CChatContextViewUI::setItemData(CModelIndex index)
	{
		CAbstractItemModel* pModel = model();
		if (!pModel)
		{
			return;
		}

		CVar var = pModel->data(index);

		if (index.column() == Column_Name)
		{
			setItemName(index, var.WideStrVal());
		}
		else if (index.column()==Column_Icon)
		{
			setItemIcon(index, var.WideStrVal());
		}
		else if (index.column() == Column_Text)
		{
			setItemText(index, var.WideStrVal());
		}
		else if (index.column() == Column_Flag)
		{
			selectItemShow(index, var.IntVal());
			return;
		}
	}

	void CChatContextViewUI::setItemName(CModelIndex index, const CDuiString& text)
	{
		CContainerUI* pLineContainer = getItemContainer(index.row());
		if (!pLineContainer)
		{
			return;
		}

		CControlUI* pCtrlLeft = pLineContainer->FindSubControl(_T("chat_msg_item_name_left"));
		CControlUI* pCtrlRight = pLineContainer->FindSubControl(_T("chat_msg_item_name_right"));
		if (pCtrlLeft&&pCtrlRight)
		{
			pCtrlLeft->SetText(text.GetData());
			pCtrlRight->SetText(text.GetData());
		}
	}
	
	void CChatContextViewUI::setItemIcon(CModelIndex index, const CDuiString& text)
	{
		CContainerUI* pLineContainer = getItemContainer(index.row());
		if (!pLineContainer)
		{
			return;
		}

		CControlUI* pCtrlLeft = pLineContainer->FindSubControl(_T("chat_msg_item_icon_left"));
		CControlUI* pCtrlRight = pLineContainer->FindSubControl(_T("chat_msg_item_icon_right"));
		if (pCtrlLeft&&pCtrlRight)
		{
			pCtrlLeft->SetBkImage(text.GetData());
			pCtrlRight->SetBkImage(text.GetData());
		}
	}

	void CChatContextViewUI::setItemText(CModelIndex index, const CDuiString& text)
	{
		CContainerUI* pLineContainer = getItemContainer(index.row());
		if (!pLineContainer)
		{
			return;
		}

		CLabelUI* pCtrlLeft = (CLabelUI*)pLineContainer->FindSubControl(_T("chat_msg_item_text_left"));
		CLabelUI* pCtrlRight = (CLabelUI*)pLineContainer->FindSubControl(_T("chat_msg_item_text_right"));
		if (pCtrlLeft&&pCtrlRight)
		{
			pCtrlLeft->SetText(text.GetData());
			pCtrlRight->SetText(text.GetData());
		}
	}

	void CChatContextViewUI::selectItemShow(CModelIndex index, int flag)
	{
		CContainerUI* pLineContainer = getItemContainer(index.row());
		CTabLayoutUI* pTabContainer = dynamic_cast<CTabLayoutUI*>(pLineContainer);
		if (!pTabContainer)
		{
			return;
		}

		pTabContainer->SelectItem(flag);
	}

	CContainerUI* CChatContextViewUI::getItemContainer(int row)
	{
		CControlUI* pLineItem = this->GetItemAt(row);
		CContainerUI* pLineContainer = dynamic_cast<CContainerUI*>(pLineItem);
		return pLineContainer;
	}

	void CChatContextViewUI::OnSetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue)
	{
		if (_tcsicmp(pstrName, _T("item_source")) == 0) {
			setItemSource(pstrValue);
		}
		else {
			CVerticalLayoutUI::OnSetAttribute(pstrName, pstrValue);
		}
	}

	CControlUI* CChatContextViewUI::addLineToView()
	{
		CAbstractItemModel* pModel = model();
		if (!pModel)
		{
			return NULL;
		}

		CPaintManagerUI* pManager = this->GetManager();
		if (!pManager)
		{
			return NULL;
		}

		CDialogBuilder builder;
		CControlUI* pItem = builder.Create(m_ItemSource.GetData(), NULL, NULL, GetManager());
		if (!pItem)
		{
			return NULL;
		}

		this->Add(pItem);

		return pItem;
	}


}
